package com.example.demo.web.controller;

import com.example.demo.domain.Menu;
import com.example.demo.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class RouterController {

    @Autowired
    private IMenuService menuService;

    //登录成功
//    @RequestMapping({"/"})
//    public String index(){
//        return "views/index";
//    }

    @RequestMapping({"/error403"})
    public String error403(){
        return "views/error/error403";
    }

    //登录页 toLogin
    @RequestMapping({"/toLogin"})
    public String toLogin(){
        return "views/login";
    }

    //查询菜单
    @RequestMapping("/main")
    public String main(HttpSession session){
        System.out.println("登录成功进入");
        if(session == null){
            return "redirect:/toLogin";
        }
        List<Menu> menuList = (List<Menu>)session.getAttribute("menuList");
        if(menuList == null) {
            //查询所有菜单
            List<Menu> menus = menuService.findAll();
            if (menus != null) {
                session.setAttribute("menuList", menus);
            }
        }
        return "views/index";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") int id){
        return "views/level1/"+id;
    }

    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") int id){
        return "views/level2/"+id;
    }

    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") int id){
        return "views/level3/"+id;
    }
}
